﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core.ProductionModules;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Unit")]

    public class UnitController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public UnitController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }

        UnitFetcher fetcher = new UnitFetcher();
        UnitGroupFetcher gfetcher = new UnitGroupFetcher();

        UnitManager manager = new UnitManager();
        UnitGroupManager gmanager = new UnitGroupManager();
        [Route("ListByG")]
        [HttpGet]
        public ResponseModel ListByGid([FromUri]long gid)
        {
            return NormalControllerHelper.ListDetail(fetcher, "BAS_PRODUCTION_LIST", gid, getInhibition);
        }
        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(fetcher, "BAS_PRODUCTION_LIST", paramModels, getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "BAS_PRODUCTION_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(UnitModel data)
        {
            return NormalControllerHelper.Set(fetcher, "BAS_PRODUCTION_SET", data, manager, setInhibition, getInhibition);
        }

        [Route("SetUnits")]
        [HttpPost]
        public ResponseModel SetDetail(RequestModel<UnitModel> data)
        {

            for (int i = 0; i < data.Data.Count(); i++)
            {
                if (data.Data[i].UnitGroupId != data.MasterId)
                {
                    data.Data[i].UnitGroupId = data.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(fetcher,fetcher, "BAS_PRODUCTION_SET", data, manager, setInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(UnitModel data)
        {
            return NormalControllerHelper.Remove("BAS_PRODUCTION_DEL", data, manager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("BAS_PRODUCTION_DEL", id, manager);
        }
        [Route("RemoveWithDetail")]
        [HttpGet]
        public ResponseModel RemoveWithDetail([FromUri]long id)
        {
            return NormalControllerHelper.RemoveWithDetail(new UnitFetcher(), "BAS_PRODUCTION_DEL", id, gmanager, manager);
        }

        [Route("GList")]
        [HttpGet]
        public ResponseModel GList([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(gfetcher, "BAS_PRODUCTION_LIST", paramModels, getInhibition);
        }
        [Route("GGet")]
        [HttpGet]
        public ResponseModel GGet(long id)
        {
            return NormalControllerHelper.Get(gfetcher, "BAS_PRODUCTION_LIST", id, getInhibition);
        }

        [Route("GSet")]
        [HttpPost]
        public ResponseModel GSet(UnitGroupModel data)
        {
            return NormalControllerHelper.Set(gfetcher, "BAS_PRODUCTION_SET", data, gmanager, setInhibition, getInhibition);
        }

        [Route("GRemove")]
        [HttpPost]
        public ResponseModel GRemove(UnitGroupModel data)
        {
            return NormalControllerHelper.Remove("BAS_PRODUCTION_DEL", data, gmanager);
        }

        [Route("GRemoveById")]
        [HttpGet]
        public ResponseModel GRemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("BAS_PRODUCTION_DEL", id, gmanager);
        }
    }
}
